<?php 
/**
 * 分类管理控制器
 */
class CategoryController extends AuthController{
	private $model;
	//自动执行方法
	public function __auto(){
		$this->model = K('Cate');
	}
	
	
	//显示分类列表
	public function index(){
		$data = $this->model->all();
		$data = Data::tree($data,'cname');
		$this->assign('data',$data);
//		p($data);die;
		$this->display();
	}
	
	//分类添加
	public function add(){
		if(IS_POST){
			//执行模型的添加
			if(!$this->model->addData()){
				$this->error($this->model->error);
			}
		$cid = Q('get.cid',0,'intval');
//		p($cid);
		//获得所属分类
		$cate = $this->model->where("cid={$cid}")->find();
//		p($cate);die;
		$this->assign('cate',$cate);
		$this->success('添加成功',U('index'));
		}
		$this->display();
	}
	
	
	//添加子分类
	public function addSon(){
		if(IS_POST){
//			p($_POST);
			if(!$this->model->addData()){
				$this->error($this->model->error);
			}
			$this->success('添加成功',U('index'));
		}
		$cid = Q('get.cid',0,'intval');
		$catedata =K('Cate');
		$catedata = $catedata->where("cid={$cid}")->find();
		
//		p($catedata);
		$this->assign('catedata',$catedata);
//		p($cid);
		$data=K('Type')->all();
//		p($data);
		$this->assign('data',$data);
		$this->display();
	}
	
	
	//删除
	public function del(){
		$cid = Q('get.cid',0,'intval');
		//先判断是否有子分类
		$sonCate = $this->model->where("pid = {$cid}")->find();
		if($sonCate){
			$this->error('请先删除子分类');
		}
		//删除分类
		$this->model->where("cid = {$cid}")->delete();
		$this->success('删除成功',U('index'),3);
	}
	
	
	//编辑
	public function edit(){
		$cid = Q('get.cid',0,'intval');
		if(IS_POST){
			if(!$this->model->editData($cid)){
				$this->error($this->model->error);
			}
			$this->success('编辑成功',U('index'));
		}
		//获得旧的数据
		$oldData = $this->model->where("cid={$cid}")->find();
//		p($oldData);die;
		$this->assign('oldData',$oldData);
		
		
		$this->display();
	}
	
	
	
	
	
}




 ?>